package com.yangchoi.lib_base.network.interceptor

import android.net.Uri
import okhttp3.Interceptor
import okhttp3.Response

// 动态设置baseurl
class DynamicBaseUrlInterceptor : Interceptor {

    companion object {
        const val Base_Url = "https://www.wanandroid.com"
    }

    private var baseUrl: String = Uri.parse(Base_Url).host ?: ""

    fun setBaseUrl(baseUrl: String) {
        val url = baseUrl
        val uri = Uri.parse(url)
        val host = uri.host ?: ""
        this.baseUrl = host
    }

    override fun intercept(chain: Interceptor.Chain): Response {
        val originalRequest = chain.request()
        val modifiedUrl = originalRequest.url.newBuilder()
            .scheme(originalRequest.url.scheme)
            .host(baseUrl)
            .build()
        val modifiedRequest = originalRequest.newBuilder()
            .url(modifiedUrl)
            .build()
        return chain.proceed(modifiedRequest)
    }
}